import React from 'react';

import Constants from '../../common/Constants';
import BaseAction from '../common/BaseAction';

export default class ActivityRequestContentAction extends BaseAction {
  constructor(props) {
    super(props);

    this.moduleType = Constants.MODULE_TYPE.ACTIVITY;
    this.name = "ActivityRequestContentAction";
  }

  call(caller, data) {
    this._baseCall(caller, data);
    this.caller.setState({
      state: 1,
    })
  }

  //activity (DqappActivity, optional),
  //is_applied (integer): 用户是否已报名，0否，1是 = ['0', '1']
  finish(data, isSucceed, response) {
    if(isSucceed) {
      var obj = {};
      var apply_fields = data.links.activity.apply_fields||[];
      for (var i = 0; i < apply_fields.length; i++) {
        const item = apply_fields[i];
        if (item.fid===0) {
          obj[''+item.field_id]=item;
        }
      }
      for (var j = 0; j < apply_fields.length; j++) {
        const item = apply_fields[j];
        if (obj[''+item.fid]!==undefined) {
          const it =obj[''+item.fid];
          if (it.fields===undefined) {
            it.fields=[];
          }
          it.fields.push(item)
          obj[''+item.fid]=it;
        }
      }
      var list =[];
      for (var x in obj) {
        list.push(obj[x]);
      }
      data.links.activity.apply_fields=list;
      this.caller.setState({
        state: 2,
        data: {activityData:data.links.activity, is_applied: data.is_applied},
        isSucceed:true
      })
    }
    else if(data){
      this.caller.setState({
        state: data.status,
      })
    }
    else {
      this.caller.setState({
        state: 3,
      })
    }
  }
}
